/**
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nightonke.boommenusample.slice;

import com.nightonke.boommenu.BoomMenuButton;
import com.nightonke.boommenusample.BuilderManager;
import com.nightonke.boommenusample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Slider;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;

public class ShareAbilitySlice extends AbilitySlice {

    private BoomMenuButton bmb1;
    private BoomMenuButton bmb2;
    private BoomMenuButton bmb3;

    private Text showDelaySeekText;
    private Text showDurationSeekText;
    private Text hideDelaySeekText;
    private Text hideDurationSeekText;

    /**
     * onStart
     * @param intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_share);
        bmb1 = (BoomMenuButton)findComponentById(ResourceTable.Id_bmb1);
        initBmb((BoomMenuButton)findComponentById(ResourceTable.Id_bmb1));
        bmb2 = (BoomMenuButton)findComponentById(ResourceTable.Id_bmb2);
        initBmbWithWhitePieceColor((BoomMenuButton)findComponentById(ResourceTable.Id_bmb2));
        bmb3 = (BoomMenuButton)findComponentById(ResourceTable.Id_bmb3);
        initBmb((BoomMenuButton)findComponentById(ResourceTable.Id_bmb3));

        bmb1.setShareLineLength(45);
        bmb1.setShareLineWidth(5);
        bmb1.setDotRadius(12);

        bmb3.setShareLine1Color(Color.BLACK);
        bmb3.setShareLine2Color(Color.BLACK);

        bmb1.buildButton();
        bmb2.buildButton();
        bmb3.buildButton();

        initShowDelaySeek();
        initShowDurationSeek();
        initHideDelaySeek();
        initHideDurationSeek();
    }

    private BoomMenuButton initBmb(BoomMenuButton bmb) {
        assert bmb != null;
        for (int i = 0; i < bmb.getButtonPlaceEnum().buttonNumber(); i++)
            bmb.addBuilder(BuilderManager.getTextInsideCircleButtonBuilder());
        return bmb;
    }

    private BoomMenuButton initBmbWithWhitePieceColor(BoomMenuButton bmb) {
        assert bmb != null;
        for (int i = 0; i < bmb.getButtonPlaceEnum().buttonNumber(); i++)
            bmb.addBuilder(BuilderManager.getTextInsideCircleButtonBuilderWithDifferentPieceColor());
        return bmb;
    }

    private void initShowDelaySeek() {
        Slider showDelaySeekBar = (Slider) findComponentById(ResourceTable.Id_show_delay_seek);
        assert showDelaySeekBar != null;
        showDelaySeekBar.setMaxValue(1000);
        showDelaySeekBar.setProgressValue((int) bmb1.getShowDelay());
        showDelaySeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean b) {
                showDelaySeekText.setText("Show delay = " + slider.getProgress() + " ms");
                bmb1.setShowDelay(progress);
                bmb2.setShowDelay(progress);
                bmb3.setShowDelay(progress);
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {

            }
        });
        showDelaySeekText = (Text)findComponentById(ResourceTable.Id_show_delay_text);
        showDelaySeekText.setText("Show delay = " + showDelaySeekBar.getProgress() + " ms");
    }

    private void initShowDurationSeek() {
        Slider showDurationSeekBar = (Slider) findComponentById(ResourceTable.Id_show_duration_seek);
        assert showDurationSeekBar != null;
        showDurationSeekBar.setMaxValue(1000);
        showDurationSeekBar.setProgressValue((int) bmb1.getShowDuration());
        showDurationSeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean b) {
                showDurationSeekText.setText("Show duration = " + slider.getProgress() + " ms");
                bmb1.setShowDuration(progress);
                bmb2.setShowDuration(progress);
                bmb3.setShowDuration(progress);
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {

            }
        });
        showDurationSeekText = (Text)findComponentById(ResourceTable.Id_show_duration_text);
        showDurationSeekText.setText("Show duration = " + showDurationSeekBar.getProgress() + " ms");
    }

    private void initHideDelaySeek() {
        Slider hideDelaySeekBar = (Slider) findComponentById(ResourceTable.Id_hide_delay_seek);
        assert hideDelaySeekBar != null;
        hideDelaySeekBar.setMaxValue(1000);
        hideDelaySeekBar.setProgressValue((int) bmb1.getHideDelay());
        hideDelaySeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean b) {
                hideDelaySeekText.setText("Hide delay = " + slider.getProgress() + " ms");
                bmb1.setHideDelay(progress);
                bmb2.setHideDelay(progress);
                bmb3.setHideDelay(progress);
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {

            }
        });
        hideDelaySeekText = (Text)findComponentById(ResourceTable.Id_hide_delay_text);
        hideDelaySeekText.setText("Hide delay = " + hideDelaySeekBar.getProgress() + " ms");
    }

    private void initHideDurationSeek() {
        Slider hideDurationSeekBar = (Slider) findComponentById(ResourceTable.Id_hide_duration_seek);
        assert hideDurationSeekBar != null;
        hideDurationSeekBar.setMaxValue(1000);
        hideDurationSeekBar.setProgressValue((int) bmb1.getHideDuration());
        hideDurationSeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean b) {
                hideDurationSeekText.setText("Hide duration = " + slider.getProgress() + " ms");
                bmb1.setHideDuration(progress);
                bmb2.setHideDuration(progress);
                bmb3.setHideDuration(progress);
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {

            }
        });
        hideDurationSeekText = (Text)findComponentById(ResourceTable.Id_hide_duration_text);
        hideDurationSeekText.setText("Hide duration = " + hideDurationSeekBar.getProgress() + " ms");
    }

    /**
     * onActive
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * onForeground
     * @param intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
